import openpyxl
from excel_pytest_allure_class.common import case

class ExcelUtils:
    def __init__(self, file_name):
        self.file_name = file_name
        self.wb = openpyxl.load_workbook(file_name)

    def read_data(self, sheet_name):
        sheet = self.wb[sheet_name]
        cases = []
        for i in range(2, sheet.max_row + 1):
            test_data = case.Case()
            test_data.id = sheet.cell(i,1).value
            test_data.module = sheet.cell(i,2).value
            test_data.description = sheet.cell(i,3).value
            test_data.method = sheet.cell(i,4).value
            test_data.url = sheet.cell(i,5).value
            test_data.params = sheet.cell(i,6).value
            test_data.header = sheet.cell(i,10).value

            test_data.expect_code = sheet.cell(i,7).value
            test_data.expect_msg = sheet.cell(i,8).value
            test_data.expect_status = sheet.cell(i,9).value
            cases.append(test_data)
        return cases

    def write_data(self, sheet_name, row, col, value):
        sheet = self.wb[sheet_name]
        sheet.cell(row, col, value)
        self.wb.save(self.file_name)



if __name__ == '__main__':
    import os
    print(os.path.abspath(__file__))
    # a = ExcelUtils('../class_test_data.xlsx').read_data('Sheet1')
    # for i in a:
    #     print(i.id)
    #     print(i.module)
    #     print(i.description)
    #     print(i.method)
    a = ExcelUtils('C:/Users/jiangwanyu/PycharmProjects/hook/excel_pytest_allure_class/class_test_data.xlsx').write_data('Sheet1', 1, 1, "我有所思在远道，一日不见兮我心悄然")
    pass